﻿using CodeSHARPer.Command;
using CodeSHARPer.Model;
using CodeSHARPer.Services;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using System.Xml.Serialization;

namespace CodeSHARPer.ViewModel.Tools
{
    public class RecentFilesViewModel : Base.ToolViewModel
    {
        public const string ToolContentId = "RecentFiles";

        public RecentFilesViewModel()
            : base("Last opened files")
        {
            ContentId = ToolContentId;

            this.OpenedFilesManager = LastOpenedFilesManager.Instance;

            this.RemoveFileCommand = new SimpleCommand<object, object>(DoRemoveFileCommand);
            this.OpenFileInContainerFolderCommand = new SimpleCommand<object, object>(DoOpenFileInContainerFolderCommand);
        }

        public override Uri IconSource
        {
            get { return new Uri("/Themes/Images/App/Ico/app.pin.small.ico", UriKind.RelativeOrAbsolute); }
        }

        #region Properties
        public LastOpenedFilesManager OpenedFilesManager { get; set; }
        #endregion Properties

        #region Commands
        public SimpleCommand<object, object> OpenFileInContainerFolderCommand { get; private set; }

        public SimpleCommand<object, object> RemoveFileCommand { get; private set; }

        #endregion Commands

        #region Methods
        internal void DoRemoveFileCommand(object args)
        {
            if (args != null && args is OpenedFile)
                this.OpenedFilesManager.RemoveFileInList(args as OpenedFile);
        }

        internal void DoOpenFileInContainerFolderCommand(object args)
        {
            if (args != null && args is OpenedFile)
                Process.Start(new ProcessStartInfo(Path.GetDirectoryName((args as OpenedFile).PathFileName)));
        }

        /// <summary>
        /// Add a file to the list
        /// </summary>
        public void AddFile(string fileName)
        {
            this.OpenedFilesManager.AddFileToList(fileName);
        }

        /// <summary>
        /// Remove a file of the list
        /// </summary>
        public void RemoveFile(string fileName)
        {
            this.OpenedFilesManager.RemoveFileInList(fileName);
        }

        #endregion Methods
    }
}
